package com.xxb.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xxb.entity.Order;
import com.xxb.util.DBHelp;
import com.xxb.util.Rowmapper;

public class OrderDao {
	private DBHelp<Order> db = new DBHelp<Order>();
	
	public int save(Order o){
		String sql = "insert into t_order(id,uid,name,address,phone,state) values(?,?,?,?,?,?)";
		return db.executeSQL(sql, o.getId(),o.getUid(),o.getName(),o.getAddress(),o.getPhone(),o.getState());
	}
	
	public List<Order> findAll(){
		String sql = "select id,uid,name,address,phone,state from t_order ";
		return db.quertyList(sql, new OrderDaoRowmapper());
	}
	
	public Order findByid(String id){
		String sql = "select id,uid,name,address,phone,state from t_order where id=?";
		return db.queryObject(sql, new OrderDaoRowmapper(),id);
	}
		
	public int updateState(String id,String state) {
		String sql = "update t_order set state = ? where id = ?";
		return db.executeSQL(sql, state,id);
	}
	public List<Order> findByuid(int uid){
		String sql = "select id,uid,name,address,phone,state from t_order where uid=?";
		return db.quertyList(sql, new OrderDaoRowmapper(),uid);
	}
	private class OrderDaoRowmapper implements Rowmapper<Order>{

		public Order mapRow(ResultSet rs) throws SQLException {
			Order o = new Order();
			
			o.setAddress(rs.getString("address"));
			o.setId(rs.getString("id"));
			o.setName(rs.getString("name"));
			o.setPhone(rs.getString("phone"));
			o.setUid(rs.getInt("uid"));
			o.setState(rs.getString("state"));
			
			return o;
		}
	}
}
